package com.icez.title_bar.style

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.TextUtils
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible

class CommonBarStyle {

    companion object {
        val instance: CommonBarStyle by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            CommonBarStyle()
        }
    }


    /**
     * 左边的图标控件
     * @sample
     * @author Icez
     */
    fun getLeftImageView(context: Context,resId:Int,imgWidth:Int,imgHeight:Int,weight:Float,paddingLeft:Int,paddingRight:Int,isVisible:Boolean,height:Int): LinearLayout {
        val mLinearLayout = LinearLayout(context)
        mLinearLayout.isVisible = isVisible
        mLinearLayout.gravity = Gravity.CENTER
        mLinearLayout.setPadding(paddingLeft,0,paddingRight,0)
        var width = 0
        if(weight<=0){
            width = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        val mLinearLayoutLayoutParams = LinearLayout.LayoutParams(width,height)
        if(weight>0){
            mLinearLayoutLayoutParams.weight = weight
        }
        mLinearLayoutLayoutParams.gravity = Gravity.CENTER_VERTICAL
        mLinearLayout.layoutParams = mLinearLayoutLayoutParams
        val leftImageView = ImageView(context)
        leftImageView.setImageResource(resId)
        var mImgWidth = imgWidth
        var mImgHeight = imgHeight
        if(mImgWidth<=0){
            mImgWidth = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        if(mImgHeight<=0){
            mImgHeight = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        val layoutParams = LinearLayout.LayoutParams(mImgWidth,mImgHeight)
        layoutParams.gravity = Gravity.CENTER
        leftImageView.layoutParams = layoutParams
        mLinearLayout.addView(leftImageView)
        return mLinearLayout
    }

    /**
     * 左边的图片右边的文字控件
     * @sample
     * @author Icez
     */
    fun getLeftImageRightTextView(context:Context,str:String,textSize:Float,textColor:Int,weight: Float,paddingLeft: Int,paddingRight: Int,isVisible:Boolean,height:Int): TextView {
        val leftImageRightTextView = TextView(context)
        leftImageRightTextView.isVisible = isVisible
        leftImageRightTextView.text = str
        leftImageRightTextView.setPadding(paddingLeft,0,paddingRight,0)
        if(textSize>0){
            leftImageRightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize)
        }
        if(textColor!=0){
            leftImageRightTextView.setTextColor(textColor)
        }
        leftImageRightTextView.gravity = Gravity.CENTER
        leftImageRightTextView.setSingleLine()
        leftImageRightTextView.setLines(1)
        leftImageRightTextView.ellipsize = TextUtils.TruncateAt.END
        leftImageRightTextView.typeface = Typeface.DEFAULT_BOLD
        var width = 0
        if(weight<=0){
            width = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        val layoutParams = LinearLayout.LayoutParams(width,height)
        if(weight>0){
            layoutParams.weight = weight
        }
        layoutParams.gravity = Gravity.CENTER_VERTICAL
        leftImageRightTextView.layoutParams = layoutParams
        return leftImageRightTextView
    }

    /**
     * 获取中间的文字控件
     * @sample
     * @author Icez
     */
    fun getCenterTextView(context:Context,str:String,textSize: Float,textColor:Int,weight:Float,isVisible:Boolean,height:Int): TextView {
        val centerTextView = TextView(context)
        centerTextView.isVisible = isVisible
        centerTextView.text = str
        if(textColor!=0){
            centerTextView.setTextColor(textColor)
        }
        if(textSize>0){
            centerTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize)
        }
        centerTextView.gravity = Gravity.CENTER
        centerTextView.setSingleLine()
        centerTextView.setLines(1)
        centerTextView.ellipsize = TextUtils.TruncateAt.END
        centerTextView.typeface = Typeface.DEFAULT_BOLD
        var width = 0
        if(weight<=0){
            width = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        val layoutParams = LinearLayout.LayoutParams(width,height)
        if(weight>0){
            layoutParams.weight = weight
        }
        layoutParams.gravity = Gravity.CENTER_VERTICAL
        centerTextView.layoutParams = layoutParams
        return centerTextView
    }

    /**
     * 获取右边的图标控件
     * @sample
     * @author Icez
     */
    fun getRightImageView(context: Context,resId:Int,imgWidth:Int,imgHeight:Int,weight:Float,paddingLeft:Int,paddingRight:Int,isVisible:Boolean,height:Int): LinearLayout {
        val mLinearLayout = LinearLayout(context)
        mLinearLayout.isVisible = isVisible
        mLinearLayout.gravity = Gravity.CENTER
        mLinearLayout.setPadding(paddingLeft,0,paddingRight,0)
        var width = 0
        if(weight<=0){
            width = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        val mLinearLayoutLayoutParams = LinearLayout.LayoutParams(width,height)
        if(weight>0){
            mLinearLayoutLayoutParams.weight = weight
        }
        mLinearLayoutLayoutParams.gravity = Gravity.CENTER_VERTICAL
        mLinearLayout.layoutParams = mLinearLayoutLayoutParams
        val leftImageView = ImageView(context)
        leftImageView.setImageResource(resId)
        var mImgWidth = imgWidth
        var mImgHeight = imgHeight
        if(mImgWidth<=0){
            mImgWidth = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        if(mImgHeight<=0){
            mImgHeight = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        val layoutParams = LinearLayout.LayoutParams(mImgWidth,mImgHeight)
        layoutParams.gravity = Gravity.CENTER
        leftImageView.layoutParams = layoutParams
        mLinearLayout.addView(leftImageView)
        return mLinearLayout
    }

    /**
     * 获取右边的图标的左边文字控件
     * @sample
     * @author Icez
     */
    fun getRightImageLeftTextView(context:Context,str:String,textSize:Float,textColor:Int,weight: Float,paddingLeft: Int,paddingRight: Int,isVisible:Boolean,height: Int): TextView {
        val rightImageRightTextView = TextView(context)
        rightImageRightTextView.isVisible = isVisible
        rightImageRightTextView.text = str
        rightImageRightTextView.setPadding(paddingLeft,0,paddingRight,0)
        if(textColor!=0){
            rightImageRightTextView.setTextColor(textColor)
        }
        if(textSize>0){
            rightImageRightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize)
        }
        rightImageRightTextView.gravity =  Gravity.CENTER
        rightImageRightTextView.setSingleLine()
        rightImageRightTextView.setLines(1)
        rightImageRightTextView.ellipsize = TextUtils.TruncateAt.END
        rightImageRightTextView.typeface = Typeface.DEFAULT_BOLD
        var width = 0
        if(weight<=0){
            width = LinearLayout.LayoutParams.WRAP_CONTENT
        }
        val layoutParams = LinearLayout.LayoutParams(width,height)

        if(weight>0){
            layoutParams.weight = weight
        }
        layoutParams.gravity = Gravity.CENTER_VERTICAL
        rightImageRightTextView.layoutParams = layoutParams
        return rightImageRightTextView
    }
}